<?php

class AdminController extends ControllerBase
{
    public function initialize()
    {
        $this->tag->setTitle('管理中心');
        parent::initialize();
    }

    public function indexAction()
    {	
    	$this->view->tests = $this->_getTestListAction();
    }

    private function _getTestListAction()
    {
    	$tests = Tests::find(array(
    		"order"  =>  "id"
    	));
    	return $tests;
    }

    public function setTestBeginAction()
    {
    	if($this->request->isGet()){
    		$id = $this->request->getQuery("id");
    		$test = Tests::findFirst($id);

    		if($test != false){
    			$test->setStatus(1);
    			$test->save();
    		}
    		$this->forward("admin/index");
    	}
    }

    public function setTestCloseAction()
    {
    	if($this->request->isGet()){
    		$id = $this->request->getQuery("id");
    		$test = Tests::findFirst($id);

    		if($test != false){
    			$test->setStatus(2);
    			$test->save();
    		}
    		$this->forward("admin/index");
    	}
    }

    public function saveChangeAction()
    {
    	if($this->request->isPost()){
    		$id = $this->request->getPost("id");
            $question = $this->request->getPost("question");
            $title = $this->request->getPost("title");
            $option_1 = $this->request->getPost("option_1");
            $option_2 = $this->request->getPost("option_2");
            $option_3 = $this->request->getPost("option_3");
            $option_4 = $this->request->getPost("option_4");
            $correct_answer = (int)$this->request->getPost("select");
            $score = (int)$this->request->getPost("score");

    		$test = TestDetails::findFirst($id);

    		if($test != false){
    			$test->setTitle($title);
                $test->setQuestion($question);
                $test->setOption_1($option_1);
                $test->setOption_2($option_2);
                $test->setOption_3($option_3);
                $test->setOption_4($option_4);
                $test->setCorrect_answer($correct_answer);
                $test->setScore($score);
    			if($test->save()){
                    echo "success";
                    return;
                }
    		}	
    	}
        echo "error";
    }

    public function saveAddAction()
    {
        if($this->request->isPost()){
            $testid = (int)$this->request->getPost("testid");
            $type = (int)$this->request->getPost("type");
            $question = $this->request->getPost("question");
            $title = $this->request->getPost("title");
            $option_1 = $this->request->getPost("option_1");
            $option_2 = $this->request->getPost("option_2");
            $option_3 = $this->request->getPost("option_3");
            $option_4 = $this->request->getPost("option_4");
            $correct_answer = (int)$this->request->getPost("select");
            $score = (int)$this->request->getPost("score");

            $test = new TestDetails();

            try{
                $test->setTest($testid);
                $test->setType($type);
                $test->setTitle($title);
                $test->setQuestion($question);
                $test->setOption_1($option_1);
                $test->setOption_2($option_2);
                $test->setOption_3($option_3);
                $test->setOption_4($option_4);
                $test->setCorrect_answer($correct_answer);
                $test->setScore($score);

                if($test->save()){
                    echo "success";
                    return;
                }
            
            }catch (Exception $e){
                echo $e->getMessage();
            }
        }
        echo "error";
    }

    public function setTestAction()
    {	
    	if($this->request->isGet ()){
    		$id = $this->request->getQuery("id");
        	$this->view->tests = $this->_getTestDetailsAction($id);
        }else{
        	$this->forward("admin/index");
        }
    }

    public function delTestAction()
    {
        if($this->request->isGet()){
            $id = $this->request->getQuery("id");
            $test = TestDetails::findFirst($id);
            if($test != false){
                if($test->delete() != false){
                    echo "success";
                    return ;
                }
            }
        }
        echo "error";
    }

    private function _getTestDetailsAction($id)
    {
    	$tests = TestDetails::find(array(
    		"test = :test:",
    		"bind"  =>  array("test"  =>  $id),
    		"order"  =>  "id"
    	));
    	return $tests;
    }
}
